package com.ncd.form.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncd.common.annotation.Log;
import com.ncd.common.core.domain.AjaxResult;
import com.ncd.common.enums.BusinessType;
import com.ncd.form.domain.*;
import com.ncd.form.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *  设计表单
 *  预览表单
 *
 * @author treenewbee
 */
@Controller
@RequestMapping("/form/display")
public class DisplayController {

    private String prefix = "form/display";

    @Resource
    ILayoutCellService layoutCellService;

    @Resource
    IFormDictService dictService;

    @Resource
    ILayoutVersionService layoutVersionService;

    @Resource
    ILayoutService layoutService;

    @Resource
    IRecordService recordService;
    /**
     * 进入设计表单布局界面
     */
    @GetMapping("/design/{versionId}")
    public String design(@PathVariable("versionId") Long versionId, ModelMap mmap)
    {

        List<LayoutCell> root=layoutCellService.display(versionId,null);
        FormDict dict=new FormDict();
        dict.setDictParent(0L);
        mmap.put("root", root);
        mmap.put("dictList",dictService.queryList(dict));
        mmap.put("versionId", versionId);
        return prefix + "/design";
    }

    /**
     * 新增保存单布局位置
     */
    @RequiresPermissions("form:display:savecell")
    @Log(title = "保存表单布局", businessType = BusinessType.INSERT)
    @PostMapping("/saveCell")
    @ResponseBody
    public AjaxResult saveCell(@RequestBody JSONObject jsonParam)
    {
        JSONArray rowJSONArr=jsonParam.getJSONArray("rows");
        Long versionId=jsonParam.getLong("versionId");
        layoutCellService.saveCell(rowJSONArr,versionId);
        return AjaxResult.success();
    }

    /**
     * 进入表单预览界面
     */
    @GetMapping("/preview/{recordId}/{versionId}/{pointer}")
    public String preview(@PathVariable("recordId") Long recordId,@PathVariable("versionId") Long versionId,@PathVariable("pointer") Long pointer, ModelMap mmap)
    {
        if(0==recordId.longValue()){
            LayoutVersion layoutVersion=layoutVersionService.getById(versionId);
            Layout layout=layoutService.getById(layoutVersion.getLayoutId());
            mmap.put("layout", layout);
            mmap.put("versionId", versionId);
            mmap.put("root", layoutCellService.display(versionId,null));
        }else{
            Record record=recordService.getById(recordId);
            LayoutVersion layoutVersion=layoutVersionService.getById(record.getRecordVersionId());
            versionId=layoutVersion.getVersionId();
            Layout layout=layoutService.getById(layoutVersion.getLayoutId());
            mmap.put("layout", layout);
            mmap.put("versionId", versionId);
            mmap.put("recordId", recordId);
            mmap.put("root", layoutCellService.display(versionId,recordId));
        }
        //具体业务表id
        mmap.put("pointer", pointer);
        return "form/display/preview";
    }
}
